/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farent.solutions.plugins.aribaweb;

import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import org.netbeans.api.project.ProjectManager;
import org.netbeans.spi.project.support.ant.AntProjectHelper;
import org.netbeans.spi.project.support.ant.EditableProperties;
import org.netbeans.spi.project.support.ant.GeneratedFilesHelper;
import org.netbeans.spi.project.support.ant.ProjectGenerator;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileSystem;
import org.openide.filesystems.FileUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 *
 * @author farouka
 */
public class NBawProjectGenerator {

    public static AntProjectHelper createProject(File dir, String name, String awhome) throws IOException {

        final FileObject dirFO = FileUtil.createFolder(dir);

        AntProjectHelper h = createProject(dirFO, name, "src", "test", awhome);
        final AWNetbeansProject p = (AWNetbeansProject) ProjectManager.getDefault().findProject(dirFO);
        ProjectManager.getDefault().saveProject(p);
        dirFO.createFolder("src"); // NOI18N
        dirFO.createFolder("test"); // NOI18N
        dirFO.createFolder("lib"); // NOI18N

        GeneratedFilesHelper gfHelper = new GeneratedFilesHelper(dirFO);
//        String resource = "org-netbeans-modules-web-nbseamgenintegration/build.xsl"; // NOI18N
//        FileObject resourceFO = Repository.getDefault().getDefaultFileSystem().findResource(resource);
//        gfHelper.generateBuildScriptFromStylesheet(GeneratedFilesHelper.BUILD_XML_PATH, resourceFO.getURL());

        gfHelper.generateBuildScriptFromStylesheet(
                GeneratedFilesHelper.BUILD_XML_PATH,
                AWNetbeansProject.class.getResource("build.xsl"));
//         ProjectClassPathModifier

        return h;
    }

    public static AntProjectHelper createProject(final FileObject dirFO, final String name,
            final String srcRoot, final String testRoot, final String awHome) throws IOException {

        final AntProjectHelper[] h = new AntProjectHelper[1];
        dirFO.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() {

            @Override
            public void run() throws IOException {
                h[0] = ProjectGenerator.createProject(dirFO, NBawConstants.PROJECT_TYPE, null);
                Element data = h[0].getPrimaryConfigurationData(true);
                Document doc = data.getOwnerDocument();

                Element nameEl = doc.createElementNS(NBawConstants.PROJECT_CONFIGURATION_NAMESPACE, "name"); // NOI18N
                nameEl.appendChild(doc.createTextNode(name));
                data.appendChild(nameEl);

                String escapedawHome = Matcher.quoteReplacement(awHome);

                Element awHomeEl = doc.createElementNS(NBawConstants.PROJECT_CONFIGURATION_NAMESPACE, "awhome"); // NOI18N
                awHomeEl.appendChild(doc.createTextNode(escapedawHome));
                data.appendChild(awHomeEl);

                EditableProperties ep = h[0].getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
                Element sourceRoots = doc.createElementNS(NBawConstants.PROJECT_CONFIGURATION_NAMESPACE, "source-roots");  //NOI18N
                if (srcRoot != null) {
                    Element root = doc.createElementNS(NBawConstants.PROJECT_CONFIGURATION_NAMESPACE, "root");   //NOI18N
                    root.setAttribute("id", "src.dir");   //NOI18N
                    sourceRoots.appendChild(root);
                    ep.setProperty("src.dir", srcRoot); // NOI18N
                }
                data.appendChild(sourceRoots);
                Element testRoots = doc.createElementNS(NBawConstants.PROJECT_CONFIGURATION_NAMESPACE, "test-roots");  //NOI18N
                if (testRoot != null) {
                    Element root = doc.createElementNS(NBawConstants.PROJECT_CONFIGURATION_NAMESPACE, "root");   //NOI18N
                    root.setAttribute("id", "test.src.dir");   //NOI18N
                    testRoots.appendChild(root);
                    ep.setProperty("test.src.dir", testRoot); // NOI18N
                }
                data.appendChild(testRoots);
                h[0].putPrimaryConfigurationData(data, true);

                ep.setProperty("javac.classpath", new String[]{ // NOI18N
                            "${aw_classpath.ariba.aribaweb.jar}:", // NOI18N
                            "${aw_classpath.ariba.metaui.jar}:", // NOI18N
                            "${aw_classpath.ariba.reload.jar}:", // NOI18N
                            "${aw_classpath.ariba.reload.jmti.jar}:", // NOI18N
                            "${aw_classpath.ariba.expr.jar}:", // NOI18N
                            "${aw_classpath.ariba.metaui.jar}:", // NOI18N
                            "${aw_classpath.ariba.metaui-jpa.jar}:", // NOI18N
                            "${aw_classpath.ariba.util.jar}:", // NOI18N
                            "${aw_classpath.ariba.widgets.jar}", // NOI18N
                        });

                ep.setProperty("dist.dir", "dist"); // NOI18N
                ep.setProperty("aw.home", awHome); // NOI18N
                ep.setProperty("build.dir", "build"); // NOI18N
                ep.setProperty("aw_classpath.ariba.aribaweb.jar", "${aw.home}/lib/ariba.aribaweb.jar"); // NOI18N
                ep.setProperty("aw_classpath.ariba.metaui.jar", "${aw.home}/lib/ariba.metaui.jar"); // NOI18N
                ep.setProperty("aw_classpath.ariba.reload.jar", "${aw.home}/lib/ariba.awreload.jar"); // NOI18N
                ep.setProperty("aw_classpath.ariba.reload.jmti.jar", "${aw.home}/lib/ariba.awreload-jmti.jar"); // NOI18N
                ep.setProperty("aw_classpath.ariba.expr.jar", "${aw.home}/lib/ariba.expr.jar"); // NOI18N
                ep.setProperty("aw_classpath.ariba.groovyloader.jar", "${aw.home}/lib/ariba.groovyloader.jar"); // NOI18N
                ep.setProperty("aw_classpath.ariba.metaui.jar", "${aw.home}/lib/ariba.metaui.jar"); // NOI18N
                ep.setProperty("aw_classpath.ariba.metaui-jpa.jar", "${aw.home}/lib/ariba.metaui-jpa.jar"); // NOI18N
                ep.setProperty("aw_classpath.ariba.util.jar", "${aw.home}/lib/ariba.util.jar"); // NOI18N
                ep.setProperty("aw_classpath.ariba.widgets.jar", "${aw.home}/lib/ariba.widgets.jar"); // NOI18N

                h[0].putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);

                ep = h[0].getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH);
                h[0].putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH, ep);
            }
        });
        return h[0];
    }
}
